package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bsh.dto.AccountNoticeQryDto;
import com.bsh.mybatis.model.AccountChangeAudit;
import com.bsh.repository.DaoAccountChangeAudit;
import com.bsh.service.AccountChangeAuditService;
import com.bsh.utils.Pager;

/**
 * 后台变动账户审核
 * 
 * @author gsh
 *
 */
@Service
@Transactional
public class AccountChangeAuditServiceImpl implements AccountChangeAuditService {
	@Autowired
	private DaoAccountChangeAudit changeAuditDao;

	/**
	 * 变动申请列表
	 * 
	 * @param pager
	 * @param qryParam
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager, AccountNoticeQryDto qryParam) {
		StringBuilder where = new StringBuilder();
		if (qryParam != null) {
			if (!StringUtils.isEmpty(qryParam.getUserName())) {
				where.append(String.format(" and user_name like '%%%s%%' ", qryParam.getUserName().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getMobile())) {
				where.append(String.format(" and mobile like '%%%s%%' ", qryParam.getMobile().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getCreateTime()) && StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and create_time >='%s' ", qryParam.getCreateTime().trim()));
			} else if (StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and create_time <='%s 23:59:59' ", qryParam.getCreateTime1().trim()));
			} else if (!StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and (create_time between '%s' and '%s 23:59:59') ",
						qryParam.getCreateTime().trim(), qryParam.getCreateTime1().trim()));
			}
			if (qryParam.getStatus()!=null && qryParam.getStatus()!=-1) {
				where.append(String.format(" and status='%s' ", qryParam.getStatus()));
			}
		}

		List<Map<String, Object>> list = changeAuditDao.getPage(pager, where.toString());
		return list;
	}

	/**
	 * 提交变动申请
	 * 
	 * @param pager
	 * @param qryParam
	 * @return
	 */
	@Override
	public Integer submitChangeAsk(AccountChangeAudit change) {
		return changeAuditDao.insert(change);
	}

	/**
	 * 经理审核列表
	 */
	@Override
	public List<Map<String, Object>> getManagerAuditPage(Pager pager, AccountNoticeQryDto qryParam) {
		StringBuilder where = new StringBuilder();
		if (qryParam != null) {
			if (!StringUtils.isEmpty(qryParam.getUserName())) {
				where.append(String.format(" and user_name like '%%%s%%' ", qryParam.getUserName().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getMobile())) {
				where.append(String.format(" and mobile like '%%%s%%' ", qryParam.getMobile().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getCreateTime()) && StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and create_time >='%s' ", qryParam.getCreateTime().trim()));
			} else if (StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and create_time <='%s 23:59:59' ", qryParam.getCreateTime1().trim()));
			} else if (!StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and (create_time between '%s' and '%s 23:59:59') ",
						qryParam.getCreateTime().trim(), qryParam.getCreateTime1().trim()));
			}
		}

		List<Map<String, Object>> list = changeAuditDao.getPage(pager, where.toString());
		return list;
	}

	/**
	 * 经理审核_通过
	 */
	@Override
	public Integer managerAuditAccountChange(Integer changeId, Integer auditor, Boolean isPass) {
		if (isPass)
			return changeAuditDao.managerAuditPass(changeId, auditor);
		else
			return changeAuditDao.managerAuditRefuse(changeId, auditor);
	}

}
